package cn.sanshuilin.kst.common.upload;

import cn.sanshuilin.kst.common.upload.util.MD5Util;
import com.alibaba.fastjson.JSONObject;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.request.UploadFileRequest;
import com.qcloud.cos.sign.Credentials;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件OSS操作
 *
 * @author zhangxd
 */
public class CosFileOperator extends AbstractFileOperator implements FileOperator {
    public static final String baseUrl = "http://sanshuilin.com/kst_img/";
    // 设置用户属性, 包括appid, secretId和SecretKey
    // 这些属性可以通过cos控制台获取(https://console.qcloud.com/cos)
    private long appId = 10070929;
    private String secretId = "AKIDjw2wgJAIhMZjjXO7tK0cycBYkkZHp9Id";
    private String secretKey = "McEh4KOGwQjuxx4T7sW4XDi174ppgNtD";
    // 设置要操作的bucket
    private String bucketName = "moon1988";


    @Override
    public void deleteFile(String realName) {
//        // 7. 删除文件
//        DelFileRequest delFileRequest = new DelFileRequest(bucketName, cosFilePath);
//        String delFileRet = cosClient.delFile(delFileRequest);
//        System.out.println("del file ret:" + delFileRet);

    }

    @Override
    public List<String> saveFile(List<MultipartFile> files) {
        // 初始化客户端配置
        ClientConfig clientConfig = new ClientConfig();
        // 设置bucket所在的区域，比如广州(gz), 天津(tj)
        clientConfig.setRegion("sh");
        // 初始化秘钥信息
        Credentials cred = new Credentials(appId, secretId, secretKey);
        // 初始化cosClient
        COSClient cosClient = new COSClient(clientConfig, cred);
        List<String> result = new ArrayList<String>();
        for (int i = 0; i < files.size(); i++) {
            MultipartFile multipartFile = files.get(i);
            // 取得当前上传文件的文件名称
            String myFileName = multipartFile.getOriginalFilename();
            if (myFileName.trim() != "") {
                try {
                    String fileName = MD5Util.random(8)
                            + MD5Util.random(8)
                            + myFileName.substring(
                            myFileName.lastIndexOf("."),
                            myFileName.length());
                    // 重命名上传后的文件名
                    String cosFilePath = "/kst_img/"
                            + fileName;
                    UploadFileRequest uploadFileRequest;

                    uploadFileRequest = new UploadFileRequest(
                            bucketName, cosFilePath, multipartFile.getBytes());

                    uploadFileRequest.setEnableShaDigest(false);
                    String uploadFileRet = cosClient
                            .uploadFile(uploadFileRequest);
                    CosUpResultBean cos_result = JSONObject.parseObject(uploadFileRet, CosUpResultBean.class);
                    if (cos_result.code == 0) {
                        result.add(baseUrl + fileName);
                    } else {
//                        LogFactory.getInstance().getLogger()
//                                .info("文件上传失败:"+cos_result );
                        return null;
                    }

                } catch (IOException e) {
//                    LogFactory.getInstance().getLogger()
//                            .info("文件上传失败:" );
                    return null;
                }

            }
        }
        // 关闭释放资源
        cosClient.shutdown();
        return result;
    }

    @Override
    public void saveFile(File file, String realName) {

    }

}
